package com.stylefeng.guns.modular.vpn_push_msg.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.dto.PushMsgDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.VpnPushMsg;
import com.stylefeng.guns.modular.vpn_push_msg.service.IVpnPushMsgService;

/**
 * 消息记录控制器
 *
 * @author fengshuonan
 * @Date 2020-03-14 11:04:42
 */
@Controller
@RequestMapping("/vpnPushMsg")
public class VpnPushMsgController extends BaseController {

    private String PREFIX = "/vpn_push_msg/vpnPushMsg/";

    @Autowired
    private IVpnPushMsgService vpnPushMsgService;

    /**
     * 跳转到消息记录首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnPushMsg.html";
    }

    /**
     * 跳转到创建推送消息
     */
    @RequestMapping("/editPushMsg")
    public String editPushMsg() {
        return PREFIX + "vpnEditPushMsg.html";
    }

    /**
     * 获取消息记录列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(String condition) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.orderBy("id",false);
        return vpnPushMsgService.selectList(wrapper);
    }

    /**
     * 创建并推送消息
     * @param pushMsgDTO
     * @return
     */
    @RequestMapping("/addPushMsg")
    @ResponseBody
    public Object addPushMsg(PushMsgDTO pushMsgDTO){
        long msgId = vpnPushMsgService.addPushMsg(pushMsgDTO);
        if(msgId > 0){
            vpnPushMsgService.pushMsg(msgId);
        }
        return new ErrorTip(500,"推送失败");
    }

//    @RequestMapping("/pushMsg")
//    @ResponseBody
//    public Object pushMsg(@RequestParam("msgId") Integer msgId){
//        return vpnPushMsgService.pushMsg(msgId);
//    }
}
